﻿using Css_Shop.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Css_Shop.Controllers
{
    /// <summary>
    /// JWT测试接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthenticationController : ControllerBase
    {
        //注入jwt服务
        private readonly IJWTService _jwtService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jwtService"></param>
        public AuthenticationController(IJWTService jwtService)
        {
            _jwtService = jwtService;
        }
        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string CreateToken(string name)
        {
            return _jwtService.CreateToken(name);
        }

        /// <summary>
        /// 根据token查询用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public string UserInfo()
        {
            //获取用户信息
            return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
        }
    }
}
